//
//  YJCDzbLogin.m
//  xiangzhe
//
//  Created by lyt on 16/11/18.
//  Copyright © 2016年 com.dazhebao. All rights reserved.
//

#import "YJCDzbLogin.h"
#import "YTTimeButton.h"
#import "YJCIsMobile.h"

@interface YJCDzbLogin ()

@property (nonatomic, strong) UITextField *accountNum;
@property (nonatomic, strong) UITextField *codeNum;
@property (nonatomic, strong) UITextField *phoneNum;
@property (nonatomic, strong) UITextField *safeCode;
@property (nonatomic, strong) YTTimeButton *sendBtn;
@end

@implementation YJCDzbLogin

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_denglu"]];

    [self setupUI];
}

- (void)loginBtnClick{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (self.phoneNum.text.length == 11 && self.safeCode.text.length > 0) {
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.Dzb_login" parameters:@{@"phone":self.phoneNum.text,@"code":self.safeCode.text,@"username":self.accountNum.text,@"userPwd":self.codeNum.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@---%@",result,error);
            [SVProgressHUD showWithStatus:@"正在登录"];
        
            NSString *resultCode = result[@"data"][@"resultCode"];

            if (resultCode.intValue == 0) {
                [SVProgressHUD dismiss];
                NSLog(@"跳转到主页");
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
                
            }else{
                NSString *mg = result[@"data"][@"resultInfo"];
                NSLog(@"网络错误 %@",mg);
                [SVProgressHUD showImage:nil status:mg];
            }
        }];
    }else{
        [SVProgressHUD showImage:nil status:@"手机号或验证码错误"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
    }
    
}
- (void)sendMessage{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    //获取到手机号 验证有效性 发送网络请求
    
    if ([YJCIsMobile stringIsMobile:self.phoneNum.text]) {
        self.sendBtn.userInteractionEnabled = YES;
        [self.sendBtn beginTime:60 title:@"重新发送" sendingColor:[UIColor lightGrayColor] finishColor:[UIColor lightGrayColor]];
        
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.SendMobileCode" parameters:@{@"phone":self.phoneNum.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@",result);
            
        }];
        
    }else{
        [SVProgressHUD showImage:nil status:@"请输入正确的手机号"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
        
    }

}
- (void)backBtnClick{

    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)setupUI{
    //标题
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"打折宝登录";
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLab];
//小标题
    UILabel *signLab = [[UILabel alloc] init];
    signLab.text = @"使用打折宝账号登录";
    signLab.font = [UIFont systemFontOfSize:13];
    signLab.textAlignment = NSTextAlignmentCenter;
    signLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:signLab];
    
    //3条分割线
    UIView *topView = [[UIView alloc] init];
    UIView *leftView = [[UIView alloc] init];
    UIView*rightView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    leftView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    rightView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    [self.view addSubview:topView];
    [self.view addSubview:leftView];
    [self.view addSubview:rightView];
    
    //账号
    UITextField *accountNum = [[UITextField alloc] init];
    accountNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    accountNum.placeholder = @"请输出打折宝用户名";
    accountNum.font = [UIFont systemFontOfSize:13];
    
    accountNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    accountNum.layer.borderWidth = 1.0;
    accountNum.layer.cornerRadius = 3.0;
    accountNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    accountNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:accountNum];
    self.accountNum = accountNum;
    accountNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    accountNum.tintColor = [UIColor blueColor];
//    accountNum.keyboardType = UIKeyboardTypePhonePad;
    // 密码
    UITextField *codeNum = [[UITextField alloc] init];
    codeNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    codeNum.placeholder = @"请输入密码";
    codeNum.font = [UIFont systemFontOfSize:13];
    codeNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    codeNum.layer.borderWidth = 1.0;
    codeNum.layer.cornerRadius = 3.0;
    codeNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    codeNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:codeNum];
    self.codeNum = codeNum;
    // 手机号
    UITextField *phoneNum = [[UITextField alloc] init];
    phoneNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    phoneNum.placeholder = @"手机号";
    phoneNum.font = [UIFont systemFontOfSize:13];
    
    phoneNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    phoneNum.layer.borderWidth = 1.0;
    phoneNum.layer.cornerRadius = 3.0;
    phoneNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    phoneNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:phoneNum];
    self.phoneNum = phoneNum;
    phoneNum.tintColor = [UIColor blueColor];
    phoneNum.keyboardType = UIKeyboardTypePhonePad;
   // 验证码
    UITextField *safeCode = [[UITextField alloc] init];
    safeCode.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    safeCode.placeholder = @"验证码";
    safeCode.font = [UIFont systemFontOfSize:13];
    
    safeCode.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    safeCode.layer.borderWidth = 1.0;
    safeCode.layer.cornerRadius = 3.0;
    safeCode.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    safeCode.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:safeCode];
    self.safeCode = safeCode;
    
    //发送验证码的按钮
    YTTimeButton *timeBtn = [YTTimeButton buttonWithType:UIButtonTypeCustom];
    [timeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timeBtn setTitleColor:[UIColor yt_colorWithHexString:@"#685d5d"] forState:UIControlStateNormal];
    timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    timeBtn.layer.borderColor = [UIColor yt_colorWithHexString:@"#ff3366"].CGColor;
    timeBtn.layer.borderWidth = 1.0;
    timeBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:timeBtn];
    [timeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    self.sendBtn = timeBtn;
    
    //手机号码登录按钮
    UIButton *phoneLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneLoginBtn setTitle:@"手机号码登录" forState:UIControlStateNormal];
    [phoneLoginBtn setTitleColor:[UIColor yt_colorWithHexString:@"#ff3366"] forState:UIControlStateNormal];
    phoneLoginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [phoneLoginBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:phoneLoginBtn];
    // 登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState: UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor yt_colorWithHexString:@"#ff3366"]];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:loginBtn];
    loginBtn.alpha = 0.45;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    // 提示字段
    UILabel *tishiLab = [[UILabel alloc] init];
    tishiLab.text = @"使用打折宝账号登录";
    tishiLab.font = [UIFont systemFontOfSize:13];
    tishiLab.textAlignment = NSTextAlignmentCenter;
    tishiLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:tishiLab];

    // 布局
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(KScreenW);
    }];
    
    [signLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(kHdistance);
        make.centerX.equalTo(self.view);
    }];

    
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.height.mas_equalTo(1);
        make.right.equalTo(signLab.mas_left).offset(-kVdistance);
    }];
    
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.left.equalTo(signLab.mas_right).offset(kVdistance);
        make.height.mas_equalTo(1);
    }];
    
    [accountNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.top.equalTo(signLab.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);

    }];
    
    [codeNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(accountNum.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.height.mas_equalTo(30);
    }];

    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kHdistance);
        make.centerY.equalTo(phoneNum);
        make.left.equalTo(phoneNum.mas_right).offset(kVdistance);
        make.width.mas_equalTo(80);
    }];
    
    [phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.top.equalTo(codeNum.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [safeCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNum.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [phoneLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(safeCode.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneLoginBtn.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
    }];
    
    [tishiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance +kVdistance);
    }];
}
@end
